/*
 * @Date: 2022-09-16 09:09:44
 * @LastEditTime: 2022-09-23 18:40:57
 */
import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false })

const whiteList = ['/login']

router.beforeEach(async(to, from, next) => {
  NProgress.start()
  document.title = getPageTitle(to.meta.title)
  // 判断是否拿到token
  if (store.state.user.token) {
    // await store.dispatch('user/getInfo')
    // 判断是否要去登录页
    if (to.path === '/login') {
      next('/')
    } else {
      // 判断是否已经拿到用户信息
      if (store.state.user.userInfo.name) {
        next()
      } else {
        try {
          await store.dispatch('user/getInfo')
          // if (to.path === '/404') {
          //   next(from.path)
          // }
          next(to)
        } catch (error) {
          // console.log(to)
          await store.dispatch('user/reset')
          next(`/login?redirect=${to.path}`)
        }
      }
    }
    NProgress.done()
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
    }
    NProgress.done()
  }
})

// router.beforeEach(async(to, from, next) => {
//   NProgress.start()
//   document.title = getPageTitle(to.meta.title)
//   // const hasToken = getToken()
//   const hasToken = store.state.user.token

//   if (hasToken) {
//     if (to.path === '/login') {
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       console.log(store.state.user.userInfo.name)
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           await store.dispatch('user/reset')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     if (whiteList.indexOf(to.path) !== -1) {
//       next()
//     } else {
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.beforeEach(async(to, from, next) => {
//   console.log(router)
//   console.log(from.path)
//   console.log(to.path)
//   console.log(store.state.user.token)
//   NProgress.start()
//   document.title = getPageTitle(to.meta.title)
//   if (store.state.user.token) {
//     if (to.path === '/login') {
//       next('/')
//     } else {
//       try {
//         await store.dispatch('user/getInfo')
//         next()
//       } catch (error) {
//         await store.dispatch('user/reset')
//         next(`/login?redirect=${to.path}`)
//       }
//     }
//     NProgress.done()
//   } else {
//     if (whiteList.indexOf(to.path) !== -1) {
//       next()
//     } else {
//       next(`/login?redirect=${to.path}`)
//     }
//     NProgress.done()
//   }
// })

router.afterEach(() => {
  NProgress.done()
})
